home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / DroneZone / DZDisplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  20.0 KB  |  751 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        DZDisplay.c
  3.  *
  4.  *    Contents:    Handles the window and its contents.
  5.  *
  6.  *    Copyright © 1996 Apple Computer, Inc.
  7.  */
  8.  
  9. #ifndef USE_DRAW_SPROCKET
  10.     #define USE_DRAW_SPROCKET        1
  11. #endif
  12.  
  13. #if USE_DRAW_SPROCKET
  14.     #define USE_PIXMAP_DRAW_CONTEXT    1
  15. #endif
  16.  
  17. #include <assert.h>
  18.  
  19. #include <Fonts.h>
  20. #include <Quickdraw.h>
  21. #include <SegLoad.h>
  22. #include <Windows.h>
  23.  
  24. #include <QD3D.h>
  25. #include <QD3DCamera.h>
  26. #include <QD3DDrawContext.h>
  27. #include <QD3DGroup.h>
  28. #include <QD3DLight.h>
  29. #include <QD3DMath.h>
  30. #include <QD3DRenderer.h>
  31. #include <QD3DShader.h>
  32. #include <QD3DView.h>
  33.  
  34. #include "DZDisplay.h"
  35. #include "DZGame.h"
  36. #include "DZInput.h"
  37. #include "DZResource.h"
  38.  
  39. #if USE_DRAW_SPROCKET
  40.     #include "DrawSprocket.h"
  41. #endif
  42.  
  43.  
  44. #if USE_DRAW_SPROCKET
  45.     static DSpContextReference    gDisplayContext            = NULL;
  46. #else
  47.     static WindowPtr        gDisplayWindow                = NULL;
  48. #endif
  49.  
  50. static Boolean                gDisplayActive                = false;
  51.  
  52. static PicHandle            gDisplayStartScreen            = NULL;
  53.  
  54. static TQ3ViewObject        gDisplayView                = NULL;
  55. static TQ3DrawContextObject    gDisplayDrawContext            = NULL;
  56. static TQ3RendererObject    gDisplayRenderer            = NULL;
  57. static TQ3CameraObject        gDisplayCamera                = NULL;
  58. static TQ3GroupObject        gDisplayLightGroup            = NULL;
  59. static TQ3LightObject        gDisplayAmbientLight        = NULL;
  60. static TQ3LightObject        gDisplayDirectionalLight    = NULL;
  61. static TQ3LightObject        gDisplayPointLight            = NULL;
  62. static TQ3ShaderObject        gDisplayIllumination        = NULL;
  63.  
  64. //• To get around an Interactive Renderer bug wherein Q3Renderer_Sync
  65. //•    crashes when called on a renderer that has not rendered.
  66. static Boolean                gDisplayHasRendered            = false;
  67.  
  68.  
  69. static void Display_RenderImage(
  70.     void);
  71.     
  72. static void Display_BeginQD(
  73.     void);
  74.     
  75. static void Display_EndQD(
  76.     void);
  77.     
  78. static void Display_GetImageArea(
  79.     TQ3Area*                outArea);
  80.  
  81. static float Display_GetImageAspectRatio(
  82.     void);
  83.  
  84.  
  85. /* =============================================================================
  86.  *        Display_Init (external)
  87.  *
  88.  *    Creates the display window, initializes QD3D view etc. to draw in it.
  89.  * ========================================================================== */
  90. void Display_Init(
  91.     void)
  92. {
  93.     OSStatus                        err = noErr;
  94.     TQ3ViewAngleAspectCameraData    viewAngleCameraData;
  95.     TQ3PointLightData                pointLightData;
  96.     TQ3ColorRGB                        white = {1.0, 1.0, 1.0};
  97.     
  98.     // Grab the start screen
  99.     gDisplayStartScreen = GetPicture(kPictID_StartScreen);
  100.     
  101.     // Create the view
  102.     gDisplayView = Q3View_New();
  103.     
  104.     // Set up where to draw
  105.     #if USE_DRAW_SPROCKET
  106.     {
  107.         DSpContextAttributes attributes;
  108.         DSpContextAttributes actual;
  109.         
  110.         DSpStartup();
  111.         
  112.         // Create the display
  113.         attributes.frequency                = 0;
  114.         attributes.displayWidth                = 512;
  115.         attributes.displayHeight            = 384;
  116.         attributes.reserved1                = 0;
  117.         attributes.reserved2                = 0;
  118.         attributes.colorNeeds                = kDSpColorNeeds_Request;
  119.         attributes.colorTable                = NULL;
  120.         attributes.contextOptions             = 0; //• kDSpContextOption_QD3DAccel
  121.         attributes.backBufferDepthMask        = kDSpDepthMask_32 | kDSpDepthMask_16;
  122.         attributes.displayDepthMask            = kDSpDepthMask_32 | kDSpDepthMask_16;
  123.         attributes.backBufferBestDepth        = 16;
  124.         attributes.displayBestDepth            = 16;
  125.         attributes.pageCount                = 1;
  126.         attributes.gameMustConfirmSwitch    = false;
  127.         attributes.reserved3[0]                = 0;
  128.         attributes.reserved3[1]                = 0;
  129.         attributes.reserved3[2]                = 0;
  130.         attributes.reserved3[3]                = 0;
  131.         
  132.         err = DSpFindBestContext(&attributes, &gDisplayContext);
  133.         if (err != noErr || gDisplayContext == NULL)
  134.         {
  135.             //• PUT UP AN ALERT INDICATING THAT THERE ISN'T A
  136.             //• DEVICE GOOD ENOUGH TO RUN ON.
  137.             ExitToShell();
  138.         }
  139.         
  140.         DSpContext_Reserve(gDisplayContext, &attributes);
  141.         DSpContext_FadeGammaOut(NULL, NULL);
  142.         DSpContext_SetState(gDisplayContext, kDSpContextState_Active);
  143.         
  144.         Display_DrawContents();
  145.         
  146.         DSpContext_FadeGammaIn(NULL, NULL);
  147.         DSpContext_GetAttributes(gDisplayContext, &actual);
  148.         
  149.         #if USE_PIXMAP_DRAW_CONTEXT
  150.         {
  151.             GWorldPtr                        theGWorld;
  152.             PixMapHandle                    pixMapHandle;
  153.             TQ3PixmapDrawContextData        pixmapDrawContextData;
  154.             
  155.             // Create the pixmap draw context with the image pointing to our back buffer
  156.             DSpContext_GetBackBuffer(gDisplayContext, kDSpBufferKind_Normal, &theGWorld);
  157.             pixMapHandle = GetGWorldPixMap(theGWorld);
  158.             
  159.             pixmapDrawContextData.drawContextData.clearImageMethod    = kQ3ClearMethodWithColor;
  160.             pixmapDrawContextData.drawContextData.clearImageColor.a    = 1.0;
  161.             pixmapDrawContextData.drawContextData.clearImageColor.r    = 0.0;
  162.             pixmapDrawContextData.drawContextData.clearImageColor.g    = 0.0;
  163.             pixmapDrawContextData.drawContextData.clearImageColor.b    = 0.0;
  164.             pixmapDrawContextData.drawContextData.paneState         = kQ3False;
  165.             pixmapDrawContextData.drawContextData.maskState         = kQ3False;
  166.             pixmapDrawContextData.drawContextData.doubleBufferState    = kQ3True;
  167.             
  168.             pixmapDrawContextData.pixmap.image        = GetPixBaseAddr(pixMapHandle);
  169.             pixmapDrawContextData.pixmap.width         = attributes.displayWidth;
  170.             pixmapDrawContextData.pixmap.height        = attributes.displayHeight;
  171.             pixmapDrawContextData.pixmap.rowBytes    = (*pixMapHandle)->rowBytes & 0x00003FFF;
  172.             pixmapDrawContextData.pixmap.pixelSize    = actual.displayBestDepth;
  173.             pixmapDrawContextData.pixmap.bitOrder    = kQ3EndianBig;
  174.             pixmapDrawContextData.pixmap.byteOrder    = kQ3EndianBig;
  175.             
  176.             switch (pixmapDrawContextData.pixmap.pixelSize)
  177.             {
  178.                 case 16:
  179.                     pixmapDrawContextData.pixmap.pixelType = kQ3PixelTypeRGB16;
  180.                 break;
  181.                 
  182.                 case 32:
  183.                     pixmapDrawContextData.pixmap.pixelType = kQ3PixelTypeRGB32;
  184.                 break;
  185.                 
  186.                 default:
  187.                     assert(0);
  188.             }
  189.             
  190.             gDisplayDrawContext = Q3PixmapDrawContext_New(&pixmapDrawContextData);
  191.         }
  192.         #else
  193.         {
  194.             TQ3MacDrawContextData            macDrawContextData;
  195.             
  196.             // Create the mac draw context with the port pointing to our back buffer
  197.             macDrawContextData.drawContextData.clearImageMethod        = kQ3ClearMethodWithColor;
  198.             macDrawContextData.drawContextData.clearImageColor.a    = 1.0;
  199.             macDrawContextData.drawContextData.clearImageColor.r    = 0.0;
  200.             macDrawContextData.drawContextData.clearImageColor.g    = 0.0;
  201.             macDrawContextData.drawContextData.clearImageColor.b    = 0.0;
  202.             macDrawContextData.drawContextData.maskState            = kQ3False;
  203.             macDrawContextData.drawContextData.paneState            = kQ3False;
  204.             macDrawContextData.drawContextData.doubleBufferState    = kQ3True;
  205.             macDrawContextData.library                                = kQ3Mac2DLibraryNone;
  206.             macDrawContextData.viewPort                                = NULL;
  207.             macDrawContextData.grafPort                                = NULL;
  208.             
  209.             GetDisplayBackBuffer(gDisplayContext, &macDrawContextData.window);
  210.         
  211.             gDisplayDrawContext = Q3MacDrawContext_New(&macDrawContextData);
  212.         }
  213.         #endif
  214.     }
  215.     #else
  216.     {
  217.         TQ3MacDrawContextData            macDrawContextData;
  218.         
  219.         // Create the window
  220.         gDisplayWindow = GetNewCWindow(kWindID_Display, NULL, (WindowPtr)(-1));
  221.         
  222.         // Create the draw context
  223.         macDrawContextData.drawContextData.clearImageMethod        = kQ3ClearMethodWithColor;
  224.         macDrawContextData.drawContextData.clearImageColor.a    = 1.0;
  225.         macDrawContextData.drawContextData.clearImageColor.r    = 0.0;
  226.         macDrawContextData.drawContextData.clearImageColor.g    = 0.0;
  227.         macDrawContextData.drawContextData.clearImageColor.b    = 0.0;
  228.         macDrawContextData.drawContextData.maskState            = kQ3False;
  229.         macDrawContextData.library                                = kQ3Mac2DLibraryNone;
  230.         macDrawContextData.drawContextData.paneState            = kQ3True;
  231.         macDrawContextData.drawContextData.doubleBufferState    = kQ3True;
  232.         macDrawContextData.window                                = (CGrafPtr) gDisplayWindow;
  233.         
  234.         Display_GetImageArea(&macDrawContextData.drawContextData.pane);
  235.     
  236.         gDisplayDrawContext = Q3MacDrawContext_New(&macDrawContextData);
  237.     }
  238.     #endif
  239.     
  240.     Q3View_SetDrawContext(gDisplayView, gDisplayDrawContext);
  241.     
  242.     // Create the renderer
  243.     #if 1
  244.         gDisplayRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive);
  245.         Q3InteractiveRenderer_SetDoubleBufferBypass(gDisplayRenderer, kQ3True);
  246.     #else
  247.         gDisplayRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireframe);
  248.     #endif
  249.     
  250.     Q3View_SetRenderer(gDisplayView, gDisplayRenderer);
  251.     
  252.     // Create the camera
  253.     viewAngleCameraData.cameraData.placement.cameraLocation.x    = 0.0;
  254.     viewAngleCameraData.cameraData.placement.cameraLocation.y    = 0.0;
  255.     viewAngleCameraData.cameraData.placement.cameraLocation.z    = 0.0;
  256.     viewAngleCameraData.cameraData.placement.pointOfInterest.x    = 1.0;
  257.     viewAngleCameraData.cameraData.placement.pointOfInterest.y    = 0.0;
  258.     viewAngleCameraData.cameraData.placement.pointOfInterest.z    = 0.0;
  259.     viewAngleCameraData.cameraData.placement.upVector.x            = 0.0;
  260.     viewAngleCameraData.cameraData.placement.upVector.y            = 1.0;
  261.     viewAngleCameraData.cameraData.placement.upVector.z            = 0.0;
  262.     viewAngleCameraData.cameraData.range.hither                    = 0.2;
  263.     viewAngleCameraData.cameraData.range.yon                    = 200.0;
  264.     viewAngleCameraData.cameraData.viewPort.origin.x            = -1.0;
  265.     viewAngleCameraData.cameraData.viewPort.origin.y            = 1.0;
  266.     viewAngleCameraData.cameraData.viewPort.width                = 2.0;
  267.     viewAngleCameraData.cameraData.viewPort.height                = 2.0;
  268.     viewAngleCameraData.fov                                        = 1.2;
  269.     viewAngleCameraData.aspectRatioXToY                            = Display_GetImageAspectRatio();
  270.  
  271.     gDisplayCamera = Q3ViewAngleAspectCamera_New(&viewAngleCameraData);
  272.     
  273.     Q3View_SetCamera(gDisplayView, gDisplayCamera);
  274.     
  275.     // Create the light group
  276.     gDisplayLightGroup = Q3LightGroup_New();
  277.     Q3View_SetLightGroup(gDisplayView, gDisplayLightGroup);
  278.     
  279.     // Create the point light
  280.     pointLightData.lightData.isOn        = kQ3True;
  281.     pointLightData.lightData.brightness    = 5.0;
  282.     pointLightData.lightData.color        = white;
  283.     pointLightData.castsShadows            = kQ3True;
  284.     pointLightData.attenuation            = kQ3AttenuationTypeInverseDistance;
  285.     pointLightData.location.x            = 0.0;
  286.     pointLightData.location.y            = 0.0;
  287.     pointLightData.location.z            = 0.0;
  288.     
  289.     gDisplayPointLight = Q3PointLight_New(&pointLightData);
  290.     
  291.     Q3Group_AddObject(gDisplayLightGroup, gDisplayPointLight);
  292.     
  293.     // Create the illumination shader
  294.     #if 1
  295.         gDisplayIllumination = Q3PhongIllumination_New();
  296.     #else
  297.         gDisplayIllumination = Q3LambertIllumination_New();
  298.     #endif
  299. }
  300.  
  301.  
  302. /* =============================================================================
  303.  *        Display_Exit (external)
  304.  *
  305.  *    Takes down the window, disposes of the QD3D stuff.
  306.  * ========================================================================== */
  307. void Display_Exit(
  308.     void)
  309. {
  310.     if (gDisplayHasRendered)
  311.     {
  312. //        Q3Renderer_Sync(gDisplayRenderer, gDisplayView);
  313.         Q3View_Sync(gDisplayView);
  314.     }
  315.     
  316.     #if USE_DRAW_SPROCKET
  317.         if(gDisplayContext != NULL)
  318.         {
  319.             DSpContext_FadeGammaOut(NULL, NULL);
  320.             DSpContext_Release(gDisplayContext);
  321.             DSpContext_SetState(gDisplayContext, kDSpContextState_Active);
  322.             DSpContext_FadeGammaIn(NULL, NULL);
  323.             
  324.             gDisplayContext = NULL;
  325.         }
  326.         
  327.         DSpShutdown();
  328.     #else
  329.         if (gDisplayWindow != NULL)
  330.         {
  331.             DisposeWindow(gDisplayWindow);
  332.             gDisplayWindow = NULL;
  333.         }
  334.     #endif
  335.     
  336.     if (gDisplayView != NULL)
  337.     {
  338.         Q3Object_Dispose(gDisplayView);
  339.         gDisplayView = NULL;
  340.     }
  341.     
  342.     if (gDisplayDrawContext != NULL)
  343.     {
  344.         Q3Object_Dispose(gDisplayDrawContext);
  345.         gDisplayDrawContext = NULL;
  346.     }
  347.     
  348.     if (gDisplayRenderer != NULL)
  349.     {
  350.         Q3Object_Dispose(gDisplayRenderer);
  351.         gDisplayRenderer = NULL;
  352.     }
  353.     
  354.     if (gDisplayCamera != NULL)
  355.     {
  356.         Q3Object_Dispose(gDisplayCamera);
  357.         gDisplayCamera = NULL;
  358.     }
  359.     
  360.     if (gDisplayLightGroup != NULL)
  361.     {
  362.         Q3Object_Dispose(gDisplayLightGroup);
  363.         gDisplayLightGroup = NULL;
  364.     }
  365.     
  366.     if (gDisplayAmbientLight != NULL)
  367.     {
  368.         Q3Object_Dispose(gDisplayAmbientLight);
  369.         gDisplayAmbientLight = NULL;
  370.     }
  371.     
  372.     if (gDisplayDirectionalLight != NULL)
  373.     {
  374.         Q3Object_Dispose(gDisplayDirectionalLight);
  375.         gDisplayDirectionalLight = NULL;
  376.     }
  377.     
  378.     if (gDisplayPointLight != NULL)
  379.     {
  380.         Q3Object_Dispose(gDisplayPointLight);
  381.         gDisplayPointLight = NULL;
  382.     }
  383.     
  384.     if (gDisplayIllumination != NULL)
  385.     {
  386.         Q3Object_Dispose(gDisplayIllumination);
  387.         gDisplayIllumination = NULL;
  388.     }
  389. }
  390.  
  391.  
  392. /* =============================================================================
  393.  *        Display_Activate (external)
  394.  *
  395.  *    Handles activation and deactivation.
  396.  * ========================================================================== */
  397. void Display_Activate(
  398.     Boolean            inActivate)
  399. {
  400.     if (gDisplayActive != inActivate)
  401.     {
  402.         gDisplayActive = inActivate;
  403.         
  404.         Input_Activate(inActivate);
  405.         
  406.         if (!gDisplayActive)
  407.         {
  408.             Game_Silence();
  409.         }
  410.         
  411.         if (gDisplayHasRendered)
  412.         {
  413. //            Q3Renderer_Sync(gDisplayRenderer, gDisplayView);
  414.             Q3View_Sync(gDisplayView);
  415.         }
  416.         
  417.         Display_DrawGrow();
  418.     }
  419. }
  420.  
  421.  
  422. /* =============================================================================
  423.  *        Display_IsActive (external)
  424.  *
  425.  *    Returns true if the game is active.
  426.  * ========================================================================== */
  427. Boolean Display_IsActive(
  428.     void)
  429. {
  430.     #if USE_DRAW_SPROCKET
  431.         return true;
  432.     #else
  433.         return gDisplayActive;
  434.     #endif
  435. }
  436.  
  437.  
  438. /* =============================================================================
  439.  *        Display_DrawGrow (external)
  440.  *
  441.  *    Draws the grow box.
  442.  * ========================================================================== */
  443. void Display_DrawGrow(
  444.     void)
  445. {
  446. #if !USE_DRAW_SPROCKET
  447.     Rect                bounds;
  448.     
  449.     SetPort(gDisplayWindow);
  450.     
  451.     // Erase the horizontal part
  452.     bounds = gDisplayWindow->portRect;
  453.     bounds.top = bounds.bottom-15;
  454.     
  455.     EraseRect(&bounds);
  456.     
  457.     // Draw the grow box, but only the horizontal scroll stuff
  458.     ClipRect(&bounds);
  459.     DrawGrowIcon(gDisplayWindow);
  460.     ClipRect(&gDisplayWindow->portRect);
  461. #endif
  462. }
  463.  
  464.  
  465. /* =============================================================================
  466.  *        Display_DrawContents (external)
  467.  *
  468.  *    Draws the contents of the window.
  469.  * ========================================================================== */
  470. void Display_DrawContents(
  471.     void)
  472. {
  473.     TQ3Area            area;
  474.     FontInfo        fontInfo;
  475.     Rect            bounds;
  476.     Rect            bounds1;
  477.     
  478.     static Str255 pausedString = "\pPaused";  //• SHOULD COME FROM RESOURCE
  479.     
  480.     switch (Game_GetState())
  481.     {
  482.         case kGameState_Playing:
  483.             Display_RenderImage();
  484.         break;
  485.         
  486.         case kGameState_Paused:
  487.             Display_RenderImage();
  488.             Display_BeginQD();
  489.             Display_GetImageArea(&area);
  490.             
  491.             TextFont(kFontIDGeneva);
  492.             TextFace(bold);
  493.             TextSize(72);
  494.             
  495.             GetFontInfo(&fontInfo);
  496.             
  497.             MoveTo(
  498.                 area.min.x + 0.5*(area.min.x + area.max.x - StringWidth(pausedString)),
  499.                 area.min.y + 0.5*(area.min.y + area.max.y + fontInfo.ascent));
  500.             
  501.             ForeColor(redColor);
  502.             DrawString(pausedString);
  503.             ForeColor(blackColor);
  504.             
  505.             Display_EndQD();
  506.         break;
  507.         
  508.         case kGameState_Stopped:
  509.             Display_BeginQD();
  510.             
  511.             Display_GetImageArea(&area);
  512.             
  513.             bounds.top    = area.min.y;
  514.             bounds.left   = area.min.x;
  515.             bounds.bottom = area.max.y;
  516.             bounds.right  = area.max.x;
  517.             
  518.             PaintRect(&bounds);
  519.             
  520.             bounds1 = (*gDisplayStartScreen)->picFrame;
  521.             OffsetRect(
  522.                     &bounds1,
  523.                     ((bounds.right-bounds.left) - (bounds1.right-bounds1.left) >> 1) - bounds1.left,
  524.                     ((bounds.bottom-bounds.top) - (bounds1.bottom-bounds1.top) >> 1) - bounds1.top);
  525.             
  526.             DrawPicture(gDisplayStartScreen, &bounds1);
  527.             
  528.             Display_EndQD();
  529.         break;
  530.     }
  531. }
  532.  
  533.  
  534. /* =============================================================================
  535.  *        Display_RenderImage (internal)
  536.  *
  537.  *    Draws the 3D part of the window.
  538.  * ========================================================================== */
  539. void Display_RenderImage(
  540.     void)
  541. {
  542.     Q3View_StartRendering(gDisplayView);
  543.     do
  544.     {
  545.         // Set up the rendering state
  546.         Q3InterpolationStyle_Submit(kQ3InterpolationStyleVertex, gDisplayView);
  547.         Q3BackfacingStyle_Submit(kQ3BackfacingStyleFlip, gDisplayView);
  548.         Q3FillStyle_Submit(kQ3FillStyleFilled, gDisplayView);
  549.         Q3Object_Submit(gDisplayIllumination, gDisplayView);
  550.         
  551.         // Draw the game contents
  552.         Game_Submit(gDisplayView);
  553.     }
  554.     while (Q3View_EndRendering(gDisplayView) == kQ3ViewStatusRetraverse);
  555.     
  556.     gDisplayHasRendered = true;
  557.     
  558. #if USE_DRAW_SPROCKET
  559.     // we don't need to swap buffers because we're single buffered
  560. #endif
  561. }
  562.  
  563.  
  564. /* =============================================================================
  565.  *        Display_Resize (external)
  566.  *
  567.  *    Called after the window has changed size.
  568.  * ========================================================================== */
  569. void Display_Resize(
  570.     void)
  571. {
  572. #if !USE_DRAW_SPROCKET
  573.     TQ3Area                    imageArea;
  574.     
  575.     // Reset the clip
  576.     ClipRect(&gDisplayWindow->portRect);
  577.     
  578.     // Change the draw context
  579.     Display_GetImageArea(&imageArea);
  580.     Q3DrawContext_SetPane(gDisplayDrawContext, &imageArea);
  581.     
  582.     // Change the camera aspect ratio
  583.     Q3ViewAngleAspectCamera_SetAspectRatio(gDisplayCamera, Display_GetImageAspectRatio());
  584. #endif
  585. }
  586.  
  587.  
  588. /* =============================================================================
  589.  *        Display_GetWindow (external)
  590.  *
  591.  *    Returns our window pointer.
  592.  * ========================================================================== */
  593. WindowPtr Display_GetWindow(
  594.     void)
  595. {
  596. #if USE_DRAW_SPROCKET
  597.     return NULL;
  598. #else
  599.     return gDisplayWindow;
  600. #endif
  601. }
  602.  
  603.  
  604. /* =============================================================================
  605.  *        Display_SetViewerPosition (external)
  606.  *
  607.  *    Moves the camera and point light based on the given info.
  608.  * ========================================================================== */
  609. void Display_SetViewerPosition(
  610.     const TQ3Point3D*        inPosition,
  611.     const TQ3Vector3D*        inDirection,
  612.     const TQ3Vector3D*        inUp)
  613. {
  614.     TQ3CameraPlacement        placement;
  615.     
  616.     assert(inPosition != NULL);
  617.     assert(inDirection != NULL);
  618.     assert(inUp != NULL);
  619.     
  620.     // Move the camera
  621.     placement.cameraLocation    = *inPosition;
  622.     placement.upVector            = *inUp;
  623.     
  624.     Q3Point3D_Vector3D_Add(inPosition, inDirection, &placement.pointOfInterest);
  625.     
  626.     Q3Camera_SetPlacement(gDisplayCamera, &placement);
  627.     
  628.     // Move the point light
  629.     Q3PointLight_SetLocation(gDisplayPointLight, inPosition);
  630. }
  631.  
  632.  
  633. /* =============================================================================
  634.  *        Display_GetViewerPosition (external)
  635.  *
  636.  *    Returns the current camera position.
  637.  * ========================================================================== */
  638. void Display_GetViewerPosition(
  639.     TQ3Point3D*                outPosition,
  640.     TQ3Vector3D*            outDirection,
  641.     TQ3Vector3D*            outUp)
  642. {
  643.     TQ3CameraPlacement        placement;
  644.     
  645.     assert(outPosition != NULL);
  646.     assert(outDirection != NULL);
  647.     assert(outUp != NULL);
  648.     
  649.     Q3Camera_GetPlacement(gDisplayCamera, &placement);
  650.     
  651.     *outPosition = placement.cameraLocation;
  652.     *outUp = placement.upVector;
  653.     
  654.     Q3Point3D_Subtract(
  655.             &placement.pointOfInterest,
  656.             &placement.cameraLocation,
  657.             outDirection);
  658. }
  659.  
  660.  
  661. /* =============================================================================
  662.  *        Display_GetImageArea (internal)
  663.  *
  664.  *    Sets outArea to the area occupied by the 3D image part of the window.
  665.  * ========================================================================== */
  666. void Display_GetImageArea(
  667.     TQ3Area*                outArea)
  668. {
  669.     assert(outArea != NULL);
  670.  
  671. #if USE_DRAW_SPROCKET
  672.     {
  673.         DSpContextAttributes attributes;
  674.         
  675.         DSpContext_GetAttributes(gDisplayContext, &attributes);
  676.         
  677.         outArea->min.x = 0;
  678.         outArea->max.x = attributes.displayWidth;
  679.         outArea->min.y = 0;
  680.         outArea->max.y = attributes.displayHeight;
  681.     }
  682. #else    
  683.     outArea->min.x = gDisplayWindow->portRect.left;
  684.     outArea->max.x = gDisplayWindow->portRect.right;
  685.     outArea->min.y = gDisplayWindow->portRect.top;
  686.     outArea->max.y = gDisplayWindow->portRect.bottom-15;
  687. #endif
  688. }
  689.  
  690.  
  691. /* =============================================================================
  692.  *        Display_GetImageAspectRatio (internal)
  693.  *
  694.  *    Returns the aspect ratio of the area occupied by the 3D image part of the
  695.  *    window.
  696.  * ========================================================================== */
  697. float Display_GetImageAspectRatio(
  698.     void)
  699. {
  700.     TQ3Area                    imageArea;
  701.     
  702.     Display_GetImageArea(&imageArea);
  703.     
  704.     return (imageArea.max.x-imageArea.min.x) / (imageArea.max.y-imageArea.min.y);
  705. }
  706.  
  707.  
  708. /* =============================================================================
  709.  *        Display_BeginQD (internal)
  710.  *
  711.  *    Sets up for doing Quickdraw drawing.
  712.  * ========================================================================== */
  713. void Display_BeginQD(
  714.     void)
  715. {
  716.     if (gDisplayHasRendered)
  717.     {
  718. //        Q3Renderer_Sync(gDisplayRenderer, gDisplayView);
  719.         Q3View_Sync(gDisplayView);
  720.     }
  721.     
  722. #if USE_DRAW_SPROCKET
  723.     {
  724.         CGrafPtr port;
  725.         
  726.         DSpContext_GetBackBuffer(gDisplayContext, kDSpBufferKind_Normal, &port);
  727.         SetPort((GrafPtr) port);
  728.     }
  729. #else    
  730.     SetPort(gDisplayWindow);
  731. #endif
  732. }
  733.  
  734.  
  735. /* =============================================================================
  736.  *        Display_EndQD (internal)
  737.  *
  738.  *    Undoes what Display_BeginQD did.
  739.  * ========================================================================== */
  740. void Display_EndQD(
  741.     void)
  742. {
  743. #if USE_DRAW_SPROCKET
  744.     // do nothing
  745. #else    
  746.     // do nothing
  747. #endif
  748. }
  749.  
  750.  
  751.